//
//  QSProblemDetailViewController.m
//  DigitalEconomy_iOS
//
//  Created by Ding on 2020/3/24.
//  Copyright © 2020 DingC. All rights reserved.
//

#import "QSProblemDetailViewController.h"
#import "QSProblemInfoCell.h"
#import "QSDealingWithProblemCell.h"
#import "QSProblemStateCell.h"
#import "QSProblemFinishContentCell.h"
#import "QSProblemDoNotAcceptCell.h"
#import "QSProblemReprotInterface.h"
#import "QSProblemDetailModel.h"
#import "QSProblemProcessingViewController.h"
@interface QSProblemDetailViewController ()
@property (nonatomic, strong)QSProblemDetailModel *detailModel;
@end
static NSString *problemStateCell = @"ProblemStateCell";
static NSString *dealingWithProblemCell = @"DealingWithProblemCell";
static NSString *problemInfoCell = @"ProblemInfoCell";
static NSString *problemFinishContentCell = @"ProblemFinishContentCell";
static NSString *problemDoNotAcceptCell = @"ProblemDoNotAcceptCell";

@implementation QSProblemDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"问题处理详情";
    [self configSubView];
    [self getProblemDetail];
    NSLog(@"id: %@", self.problemId);
//    if(self.isDealFinish) {//是从处理页过来的话 直接返回到问题处理页
//        UIBarButtonItem *backBtn = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"ic_navigation_back"] target:self action:@selector(backAction:)];
//        self.navigationItem.leftBarButtonItem = backBtn;
//    }
}

- (void)backAction:(UIBarButtonItem *)sender{
    for (UIViewController *controller in self.navigationController.viewControllers) {
        if ([controller isKindOfClass:[QSProblemProcessingViewController class]]) {
            QSProblemProcessingViewController *vc = (QSProblemProcessingViewController *)controller;
            [vc.magicController switchToPage:1 animated:YES];
            [self.navigationController popToViewController:vc animated:YES];
            break;
        }
    }
}
- (void)configSubView{
    self.tableView.estimatedRowHeight = 40;
    self.tableView.frame = CGRectMake(0, 0, kScreenWidth, kScreenHeight - kNavgationBarHeight);
    [self.tableView registerNib:[UINib nibWithNibName:@"QSProblemStateCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:problemStateCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"QSProblemFinishContentCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:problemFinishContentCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"QSProblemInfoCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:problemInfoCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"QSDealingWithProblemCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:dealingWithProblemCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"QSProblemDoNotAcceptCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:problemDoNotAcceptCell];
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"#F1F3F4"];
    self.view.backgroundColor = [UIColor colorWithHexString:@"#F1F3F4"];
}

- (void)getProblemDetail{
    [QSAppKeyWindow showIndeterminateHudWithText:@"加载中"];
    [QSProblemReprotInterface getApiProblemDetailWithID:self.problemId.integerValue andBlock:^(ResponseMessage * _Nonnull rspStatusAndMessage, QSProblemDetailModel * _Nonnull result) {
        [QSAppKeyWindow hideHud];
        if (rspStatusAndMessage.code == kResponseSuccessCode) {
            self.detailModel = result;
            [self.tableView reloadData];
        }
    }];
}

#pragma mark - **************** UITableView Delegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  if (!self.detailModel) {
    return 0;
  }
  
  if (self.detailModel.state.integerValue == 1) {
    //待处理
    return 2;
  }else{
    return 3;
  }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        QSProblemStateCell *cell = [tableView dequeueReusableCellWithIdentifier:problemStateCell];
        cell.model = self.detailModel;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if (indexPath.row == 1){
        if (_detailModel.state.integerValue == 1) {
            //1待处理
            QSProblemInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:problemInfoCell];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.model = self.detailModel;
            return cell;
        }else if(_detailModel.state.integerValue == 2){
            //2处理中
            QSDealingWithProblemCell *cell = [tableView dequeueReusableCellWithIdentifier:dealingWithProblemCell];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.model = self.detailModel;
            return cell;
        }else if(_detailModel.state.integerValue == 3){
            //3不处理
            QSProblemDoNotAcceptCell *cell = [tableView dequeueReusableCellWithIdentifier:problemDoNotAcceptCell];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.model = self.detailModel;
            return cell;
        }else{
            //4已办结
            QSProblemFinishContentCell *cell = [tableView dequeueReusableCellWithIdentifier:problemFinishContentCell];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.model = self.detailModel;
            return cell;
        }
    }else{
        QSProblemInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:problemInfoCell];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.model = self.detailModel;
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
//    QSProblemDetailModel *model = self.detailModel;
    if (indexPath.row == 0) {
        return kRealValue(75 + 10);
    }
//    else if(indexPath.row == 1){
//        if (self.detailModel.state.integerValue == 1) {
//            //待处理
//            return [tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[QSProblemInfoCell class] contentViewWidth:kScreenWidth - 20];
//        }else if(self.detailModel.state.integerValue == 2){
//            //处理中
//            return [tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[QSDealingWithProblemCell class] contentViewWidth:kScreenWidth - 20];
//        }else if (self.detailModel.state.integerValue == 3){
//            //不处理
//            return [tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[QSProblemDoNotAcceptCell class] contentViewWidth:kScreenWidth - 20];
//        }else{
//            //已办结
//            return [tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[QSProblemFinishContentCell class] contentViewWidth:kScreenWidth - 20];
//        }
//    }else{
//        return [tableView cellHeightForIndexPath:indexPath model:model keyPath:@"model" cellClass:[QSProblemInfoCell class] contentViewWidth:kScreenWidth - 20];
//    }
  return UITableViewAutomaticDimension;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
